package com.maduar.mybatis.demo.util;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * Description: 服务间调用，统一返回结果
 *
 * @author danny.pang danny.pang@ebizprise.com
 * @date 2019-10-31 14:36
 *
 */
//@ApiModel("返回结果")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@ToString
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -3561929535525014561L;

    @JSONField(ordinal = 1)
//    @ApiModelProperty("调用结果")
    private boolean status;

    @JSONField(ordinal = 2)
//    @ApiModelProperty("数据")
    private T data;

    @JSONField(ordinal = 3)
//    @ApiModelProperty("错误信息")
    private ErrorInfo errorInfo;

    public static <T> Result<T> success(@NotNull T data) {
        return new Result<T>(true, data, null);
    }

    public static <T> Result<T> error(@NotNull T data, ErrorInfo errorInfo) {
        return new Result<T>(false, data, errorInfo);
    }

    public static <T> Result<T> error(ErrorInfo errorInfo) {
        return new Result<T>(false, null, errorInfo);
    }

    public static <T> Result<T> error() {
        return new Result<T>(false, null, null);
    }

    public static <T> Result<T> defaultError() {
        return new Result<T>(false, null, ErrorInfo.defaultError());
    }

    public static <T> Result<T> defaultSuccess() {
        return new Result<T>(true, null, null);
    }
}
